package com.saxon.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteCookie extends HttpServlet {
    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType ("text/html;charset=utf-8");
        Cookie[] cookies = req.getCookies ();
        Cookie new_cookie=null;
        for (Cookie cookie : cookies) {
            if (cookie.getName ().equals ("saxon")){
                new_cookie=cookie;
                resp.getWriter ().print (cookie);
            }
        }
        if (new_cookie!=null){
            new_cookie.setMaxAge (0);
        }
        resp.addCookie (new_cookie);
    }

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet (req, resp);
    }
}
